﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using AliboboStockLib.linq;
using AliboboStockLib.util;

namespace AliboboStockLib.stockupdater
{
    public class StockCodeManager
    {
        protected static Hashtable stockCodes=null;

        protected static Hashtable getCodesMap()
        {
            if (stockCodes == null)
            {
                MyStockDataContext context=LinqAgent.getContext();
                stockCodes = new Hashtable();
                try
                {
                    List<StockIndex> stockIndexs = (from stockIndex in context.StockIndexes select stockIndex).ToList<StockIndex>();
                    foreach (StockIndex idx in stockIndexs)
                    {
                        StockIndex local = new StockIndex();
                        local.id = idx.id;
                        local.代號 = idx.代號;
                        local.市場 = idx.市場;
                        local.名稱 = idx.名稱;
                        stockCodes.Add(local.代號, local);
                    }
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine(e.Message);
                }
            }
            return stockCodes;
        }
        /**************************************************************************
         * 
         * 
         * *************************************************************************/
        public static bool getStockCode(String code, ref Guid id)
        {
            if (getCodesMap().Contains(code))
            {
                StockIndex si = (StockIndex)getCodesMap()[code];
                id = si.id;
                return true;
            }
            return false;
        }
        public static void updateTse(String code)
        {
            String sql = "update StockIndex set 市場 = 'tse' where 代號='" + code + "'";
            MyStockDataContext context = LinqAgent.getContext();
            context.ExecuteCommand(sql);

        }
        /**************************************************************************
         * 
         * 
         * *************************************************************************/
        public static Guid createOtcStockCode(String code, String name, long 發行股數)
        {
            Guid result = Guid.Empty;
            if (getStockCode(code, ref result))
            {
                return result;
            }

            StockIndex id = new StockIndex();
            id.id = NewGUID.create("stki");
            id.代號 = code;
            id.名稱 = name;
            id.市場 = "otc";
            id.發行量 = 發行股數;
            MyStockDataContext context = LinqAgent.getContext();
            context.StockIndexes.InsertOnSubmit(id);
            context.SubmitChanges();
            getCodesMap().Add(code, id);
            return id.id;


        }
        public static Guid createTSEStockCode(String code, String name)
        {
            Guid result=Guid.Empty;
            if (getStockCode(code, ref result))
            {
                return result;
            }
            StockIndex id = new StockIndex();
            id.id = NewGUID.create("stki");
            id.代號 = code;
            id.名稱 = name;
            id.市場 = "tse";
            MyStockDataContext context = LinqAgent.getContext();
            context.StockIndexes.InsertOnSubmit(id);
            context.SubmitChanges();
            getCodesMap().Add(code, id);
            return id.id;
        }


    }
}
